//
//  MSReleaseFooterViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/15.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSReleaseFooterViewController.h"
#import "MSReleaseFooterTableVC.h"
#import "MSAddPhotosView.h"

@interface MSReleaseFooterViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) SXTextView             *remarkTextView;
@property (nonatomic, strong) UILabel                *indicatorLabel;
//上传图片视图
@property (nonatomic, strong) MSAddPhotosView        *addPhotoView;
@property (nonatomic, strong) UIButton               *anonymityBtn;
@property (nonatomic, strong) UIButton               *permissionBtn;
@property (nonatomic, strong) GXButton               *doneBtn;
@property (nonatomic, strong) UIView                 *footerView;
@property (nonatomic, strong) UIView                 *bottomView;
@property (nonatomic, strong) MSReleaseFooterTableVC *tableVC;
@end

@implementation MSReleaseFooterViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    titleLable.text = @"发布足迹";
    leftBarBtn.hidden = NO;
}

- (void)initUI{
    self.tableVC = [[MSReleaseFooterTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    _footerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableFooterView = _footerView;
    
    self.remarkTextView = [[SXTextView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
    self.remarkTextView.font = HeitiFont(14);
    self.remarkTextView.placeholder = @"请填写足迹";
    self.remarkTextView.delegate = (id)self;
    self.remarkTextView.backgroundColor = [UIColor whiteColor];
    [_footerView addSubview:self.remarkTextView];
    
    _indicatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-72, 110-25, 60, 20)];
    _indicatorLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13];
    _indicatorLabel.textAlignment = NSTextAlignmentRight;
    _indicatorLabel.textColor = UIColorFromRGB(0x333333);
    _indicatorLabel.text = @"100/100";
    [_footerView addSubview:_indicatorLabel];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, _remarkTextView.bottom, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_footerView addSubview:horizonLineView];
    
    self.addPhotoView = [[MSAddPhotosView alloc] initWithFrame:CGRectMake(0, horizonLineView.bottom, SCREEN_WIDTH, 100)];
    //每行最多显示三张图片
    self.addPhotoView.columeCount = 3;
    //图片的宽高比
//    self.addPhotoView.heightWidthRatio = 70/85.0;
    //最大上传数量
    self.addPhotoView.maxPhotoCount = 6;
    //隐藏上传图片控件的footerView
    self.addPhotoView.hiddenFooterView = YES;
    self.addPhotoView.delegate = (id)self;
    self.addPhotoView.bgColor = kDefaultViewBackgroundColor;
    self.addPhotoView.dataArray = [NSMutableArray array];
    [_footerView addSubview:self.addPhotoView];
    
    _bottomView =[[UIView alloc] initWithFrame:CGRectMake(0, _addPhotoView.bottom, SCREEN_WIDTH, 80)];
    _bottomView.backgroundColor = kDefaultViewBackgroundColor;
    [_footerView addSubview:_bottomView];
    
    self.anonymityBtn = [self creatBtnWithFrame:CGRectMake(10, 15, 95, 20) andTitle:@"匿名发布"];
    self.permissionBtn = [self creatBtnWithFrame:CGRectMake(self.anonymityBtn.right+20, self.anonymityBtn.top, 100, 20) andTitle:@"只有我能看"];
    
    self.doneBtn = [[GXButton alloc] initWithFrame:CGRectMake(12, self.anonymityBtn.bottom + 20, SCREEN_WIDTH-24, 45)];
    self.doneBtn.titleLabel.font = HeitiFont(16);
    [self.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.doneBtn.backgroundColor = kBlueColor;
    self.doneBtn.layer.cornerRadius = 2;
    [self.doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:self.doneBtn];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShownLogin:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHiddenLogin:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChangeNotification:) name:UITextViewTextDidChangeNotification object:nil];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    tapGesture.delegate = self;
    [self.contentView addGestureRecognizer:tapGesture];
}

- (UIButton *)creatBtnWithFrame:(CGRect)frame andTitle:(NSString*)title {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.titleLabel.font = HeitiFont(14);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"select0"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"select1"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:btn];
    return btn;
}
- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"UploadImage"]!=nil){
            NSMutableArray *imageId = [NSMutableArray arrayWithCapacity:0];
            NSArray *pictureArray = responseDic[@"UploadImage"];
            for (propertyPicture *pictureModel in pictureArray) {
                [imageId addObject:[HDUtil check:pictureModel.cateId]];
            }
            NSString *imageStr = [imageId componentsJoinedByString:@","];
            NSString *anonymous = self.anonymityBtn.selected?@"1":@"0";
            NSString *visual = self.permissionBtn.selected?@"1":@"0";
            NSDictionary *dict = @{@"projectId":[HDUtil check:_projectId],
                                   @"content":self.remarkTextView.text,
                                   @"anonymous":anonymous,
                                   @"visual":visual,
                                   @"imgIds":imageStr,
                                   };
            [netEngine requestReleaseFooterWithTarget:self.VctrID hudView:self.contentView params:dict];
        }
        if (responseDic[@"ReleaseFooter"]!=nil){
            [HDUtil showMessage:@"发布成功"];
            [rootNavigation popViewControllerAnimated:YES];
            if (self.choickBlock) {
                self.choickBlock();
            }
        }
    }
}

- (void)textViewTextDidChangeNotification:(NSNotification*)aNotification{
    _indicatorLabel.text = [NSString stringWithFormat:@"%ld/100",100-self.remarkTextView.text.length];
}

#pragma mark - MSAddPhotosView Delegate
- (void)photoCollectionViewUpdate
{
    _bottomView.top = _addPhotoView.bottom;
    _footerView.height = 250+self.addPhotoView.height;
    self.tableVC.tableView.tableFooterView = _footerView;
}

- (void)selectClick:(UIButton*)btn{
    btn.selected = !btn.selected;
    if (self.anonymityBtn.selected) {//匿名
        self.permissionBtn.enabled = NO;
    }
    else{
        self.permissionBtn.enabled = YES;
    }
    
    if (self.permissionBtn.selected) {//自己
        self.anonymityBtn.enabled = NO;
    }
    else{
        self.anonymityBtn.enabled = YES;
    }
}

- (void)doneClick{
    if (self.anonymityBtn.selected) {
        NSLog(@"1选中");
    }
    else{
        NSLog(@"1不选中");
    }
    if (self.permissionBtn.selected) {
        NSLog(@"2选中");
    }
    else{
        NSLog(@"2不选中");
    }
    NSString *anonymous = self.anonymityBtn.selected?@"1":@"0";
    NSString *visual = self.permissionBtn.selected?@"1":@"0";
    if ([[self.remarkTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){
        [HDUtil showMessage:@"文字足迹必填"];
        return;
    }
    NSMutableArray *tmpDataArray = [NSMutableArray arrayWithArray:self.addPhotoView.dataArray];
    [tmpDataArray removeLastObject];

    if (tmpDataArray.count) {
        [netEngine requestUploadImageWithTarget:self.VctrID hudView:self.contentView params:tmpDataArray];
    }
    else{
        NSDictionary *dict = @{@"projectId":[HDUtil check:_projectId],
                               @"content":self.remarkTextView.text,
                               @"anonymous":anonymous,
                               @"visual":visual,
                               @"imgIds":@"",
                               };
        [netEngine requestReleaseFooterWithTarget:self.VctrID hudView:self.contentView params:dict];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSInteger newLength = textView.text.length+text.length-range.length;
    if (!text.length || newLength<=100) {
        return YES;
    } else {
        return NO;
    }
}

//键盘显示的时候的处理
- (void)keyboardWasShownLogin:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    _tableVC.tableView.height = SCREEN_HEIGHT-KNavBarHeight-kbSize.height;
}

//键盘消失的时候的处理
- (void)keyboardWillBeHiddenLogin:(NSNotification*)aNotification
{
    _tableVC.tableView.height = SCREEN_HEIGHT-KNavBarHeight;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view.superview.superview isKindOfClass:[UICollectionViewCell class]]) {
        return NO;
    }
    else{
        return YES;
    }
}

- (void)tapClick{
    [self.view endEditing:YES];
}

@end
